﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Helldivers_Alpha.DataFrom
{
    public partial class SetFrom : Form
    {
        public SetFrom()
        {
            InitializeComponent();
        }

        private void SetFrom_Load(object sender, EventArgs e)
        {
            textBox1.Text = Properties.Settings.Default.RegistHotKey01;
            textBox2.Text = Properties.Settings.Default.RegistHotKey02;
            textBox3.Text = Properties.Settings.Default.RegistHotKey03;
            textBox4.Text = Properties.Settings.Default.RegistHotKey04;
            textBox5.Text = Properties.Settings.Default.RegistHotKey05;
            textBox6.Text = Properties.Settings.Default.RegistHotKey06;
            textBox7.Text = Properties.Settings.Default.RegistHotKey07;
            textBox8.Text = Properties.Settings.Default.RegistHotKey08;
            textBox9.Text = Properties.Settings.Default.RegistHotKey09;
            textBox10.Text = Properties.Settings.Default.DelayTime.ToString();
        }

        private void TextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;  //Suppress the key from being processed by the underlying control.
            textBox1.Text = string.Empty;  //Empty the content of the textbox

            //Set the backspace button to specify that the user does not want to use a shortcut.
            if (e.KeyData == Keys.Back)
            {
                textBox1.Text = Keys.None.ToString();
                return;
            }

            //Make the user specify a modifier. Control, Alt or Shift.
            //If a modifier is not present then clear the textbox.
            if (e.Modifiers == Keys.None)
            {
                MessageBox.Show("必须指定“control”、“alt”或“shift”等");
                textBox1.Text = Keys.None.ToString();
                return;
            }

            //A modifier is present. Process each modifier.
            //Modifiers are separated by a ",". So we'll split them and write each one to the textbox.
            foreach (string modifier in e.Modifiers.ToString().Split(new Char[] { ',' }))
            {
                textBox1.Text += modifier + " + ";
            }

            //KEYCODE contains the last key pressed by the user.
            //If KEYCODE contains a modifier, then the user has not entered a shortcut. hence, KeyisSet is false
            //But if not, KeyisSet is true.
            if (e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.ControlKey | e.KeyCode == Keys.Menu)
            {
                //KeyisSet = false;
            }
            else
            {
                textBox1.Text += e.KeyCode.ToString();
            }
        }

        private void TextBox2_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;  //Suppress the key from being processed by the underlying control.
            textBox2.Text = string.Empty;  //Empty the content of the textbox

            //Set the backspace button to specify that the user does not want to use a shortcut.
            if (e.KeyData == Keys.Back)
            {
                textBox2.Text = Keys.None.ToString();
                return;
            }

            //Make the user specify a modifier. Control, Alt or Shift.
            //If a modifier is not present then clear the textbox.
            if (e.Modifiers == Keys.None)
            {
                MessageBox.Show("必须指定“control”、“alt”或“shift”等");
                textBox2.Text = Keys.None.ToString();
                return;
            }

            //A modifier is present. Process each modifier.
            //Modifiers are separated by a ",". So we'll split them and write each one to the textbox.
            foreach (string modifier in e.Modifiers.ToString().Split(new Char[] { ',' }))
            {
                textBox2.Text += modifier + " + ";
            }

            //KEYCODE contains the last key pressed by the user.
            //If KEYCODE contains a modifier, then the user has not entered a shortcut. hence, KeyisSet is false
            //But if not, KeyisSet is true.
            if (e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.ControlKey | e.KeyCode == Keys.Menu)
            {
                //KeyisSet = false;
            }
            else
            {
                textBox2.Text += e.KeyCode.ToString();
            }
        }

        private void TextBox3_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;  //Suppress the key from being processed by the underlying control.
            textBox3.Text = string.Empty;  //Empty the content of the textbox

            //Set the backspace button to specify that the user does not want to use a shortcut.
            if (e.KeyData == Keys.Back)
            {
                textBox3.Text = Keys.None.ToString();
                return;
            }

            //Make the user specify a modifier. Control, Alt or Shift.
            //If a modifier is not present then clear the textbox.
            if (e.Modifiers == Keys.None)
            {
                MessageBox.Show("必须指定“control”、“alt”或“shift”等");
                textBox3.Text = Keys.None.ToString();
                return;
            }

            //A modifier is present. Process each modifier.
            //Modifiers are separated by a ",". So we'll split them and write each one to the textbox.
            foreach (string modifier in e.Modifiers.ToString().Split(new Char[] { ',' }))
            {
                textBox3.Text += modifier + " + ";
            }

            //KEYCODE contains the last key pressed by the user.
            //If KEYCODE contains a modifier, then the user has not entered a shortcut. hence, KeyisSet is false
            //But if not, KeyisSet is true.
            if (e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.ControlKey | e.KeyCode == Keys.Menu)
            {
                //KeyisSet = false;
            }
            else
            {
                textBox3.Text += e.KeyCode.ToString();
            }
        }

        private void TextBox4_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;  //Suppress the key from being processed by the underlying control.
            textBox4.Text = string.Empty;  //Empty the content of the textbox

            //Set the backspace button to specify that the user does not want to use a shortcut.
            if (e.KeyData == Keys.Back)
            {
                textBox4.Text = Keys.None.ToString();
                return;
            }

            //Make the user specify a modifier. Control, Alt or Shift.
            //If a modifier is not present then clear the textbox.
            if (e.Modifiers == Keys.None)
            {
                MessageBox.Show("必须指定“control”、“alt”或“shift”等");
                textBox4.Text = Keys.None.ToString();
                return;
            }

            //A modifier is present. Process each modifier.
            //Modifiers are separated by a ",". So we'll split them and write each one to the textbox.
            foreach (string modifier in e.Modifiers.ToString().Split(new Char[] { ',' }))
            {
                textBox4.Text += modifier + " + ";
            }

            //KEYCODE contains the last key pressed by the user.
            //If KEYCODE contains a modifier, then the user has not entered a shortcut. hence, KeyisSet is false
            //But if not, KeyisSet is true.
            if (e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.ControlKey | e.KeyCode == Keys.Menu)
            {
                //KeyisSet = false;
            }
            else
            {
                textBox4.Text += e.KeyCode.ToString();
            }
        }

        private void TextBox5_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;  //Suppress the key from being processed by the underlying control.
            textBox5.Text = string.Empty;  //Empty the content of the textbox

            //Set the backspace button to specify that the user does not want to use a shortcut.
            if (e.KeyData == Keys.Back)
            {
                textBox5.Text = Keys.None.ToString();
                return;
            }

            //Make the user specify a modifier. Control, Alt or Shift.
            //If a modifier is not present then clear the textbox.
            if (e.Modifiers == Keys.None)
            {
                MessageBox.Show("必须指定“control”、“alt”或“shift”等");
                textBox5.Text = Keys.None.ToString();
                return;
            }

            //A modifier is present. Process each modifier.
            //Modifiers are separated by a ",". So we'll split them and write each one to the textbox.
            foreach (string modifier in e.Modifiers.ToString().Split(new Char[] { ',' }))
            {
                textBox5.Text += modifier + " + ";
            }

            //KEYCODE contains the last key pressed by the user.
            //If KEYCODE contains a modifier, then the user has not entered a shortcut. hence, KeyisSet is false
            //But if not, KeyisSet is true.
            if (e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.ControlKey | e.KeyCode == Keys.Menu)
            {
                //KeyisSet = false;
            }
            else
            {
                textBox5.Text += e.KeyCode.ToString();
            }
        }

        private void TextBox6_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;  //Suppress the key from being processed by the underlying control.
            textBox6.Text = string.Empty;  //Empty the content of the textbox

            //Set the backspace button to specify that the user does not want to use a shortcut.
            if (e.KeyData == Keys.Back)
            {
                textBox6.Text = Keys.None.ToString();
                return;
            }

            //Make the user specify a modifier. Control, Alt or Shift.
            //If a modifier is not present then clear the textbox.
            if (e.Modifiers == Keys.None)
            {
                MessageBox.Show("必须指定“control”、“alt”或“shift”等");
                textBox6.Text = Keys.None.ToString();
                return;
            }

            //A modifier is present. Process each modifier.
            //Modifiers are separated by a ",". So we'll split them and write each one to the textbox.
            foreach (string modifier in e.Modifiers.ToString().Split(new Char[] { ',' }))
            {
                textBox6.Text += modifier + " + ";
            }

            //KEYCODE contains the last key pressed by the user.
            //If KEYCODE contains a modifier, then the user has not entered a shortcut. hence, KeyisSet is false
            //But if not, KeyisSet is true.
            if (e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.ControlKey | e.KeyCode == Keys.Menu)
            {
                //KeyisSet = false;
            }
            else
            {
                textBox6.Text += e.KeyCode.ToString();
            }
        }

        private void TextBox7_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;  //Suppress the key from being processed by the underlying control.
            textBox7.Text = string.Empty;  //Empty the content of the textbox

            //Set the backspace button to specify that the user does not want to use a shortcut.
            if (e.KeyData == Keys.Back)
            {
                textBox7.Text = Keys.None.ToString();
                return;
            }

            //Make the user specify a modifier. Control, Alt or Shift.
            //If a modifier is not present then clear the textbox.
            if (e.Modifiers == Keys.None)
            {
                MessageBox.Show("必须指定“control”、“alt”或“shift”等");
                textBox7.Text = Keys.None.ToString();
                return;
            }

            //A modifier is present. Process each modifier.
            //Modifiers are separated by a ",". So we'll split them and write each one to the textbox.
            foreach (string modifier in e.Modifiers.ToString().Split(new Char[] { ',' }))
            {
                textBox7.Text += modifier + " + ";
            }

            //KEYCODE contains the last key pressed by the user.
            //If KEYCODE contains a modifier, then the user has not entered a shortcut. hence, KeyisSet is false
            //But if not, KeyisSet is true.
            if (e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.ControlKey | e.KeyCode == Keys.Menu)
            {
                //KeyisSet = false;
            }
            else
            {
                textBox7.Text += e.KeyCode.ToString();
            }
        }

        private void TextBox8_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;  //Suppress the key from being processed by the underlying control.
            textBox8.Text = string.Empty;  //Empty the content of the textbox

            //Set the backspace button to specify that the user does not want to use a shortcut.
            if (e.KeyData == Keys.Back)
            {
                textBox8.Text = Keys.None.ToString();
                return;
            }

            //Make the user specify a modifier. Control, Alt or Shift.
            //If a modifier is not present then clear the textbox.
            if (e.Modifiers == Keys.None)
            {
                MessageBox.Show("必须指定“control”、“alt”或“shift”等");
                textBox8.Text = Keys.None.ToString();
                return;
            }

            //A modifier is present. Process each modifier.
            //Modifiers are separated by a ",". So we'll split them and write each one to the textbox.
            foreach (string modifier in e.Modifiers.ToString().Split(new Char[] { ',' }))
            {
                textBox8.Text += modifier + " + ";
            }

            //KEYCODE contains the last key pressed by the user.
            //If KEYCODE contains a modifier, then the user has not entered a shortcut. hence, KeyisSet is false
            //But if not, KeyisSet is true.
            if (e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.ControlKey | e.KeyCode == Keys.Menu)
            {
                //KeyisSet = false;
            }
            else
            {
                textBox8.Text += e.KeyCode.ToString();
            }
        }

        private void TextBox9_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;  //Suppress the key from being processed by the underlying control.
            textBox9.Text = string.Empty;  //Empty the content of the textbox

            //Set the backspace button to specify that the user does not want to use a shortcut.
            if (e.KeyData == Keys.Back)
            {
                textBox9.Text = Keys.None.ToString();
                return;
            }

            //Make the user specify a modifier. Control, Alt or Shift.
            //If a modifier is not present then clear the textbox.
            if (e.Modifiers == Keys.None)
            {
                MessageBox.Show("必须指定“control”、“alt”或“shift”等");
                textBox9.Text = Keys.None.ToString();
                return;
            }

            //A modifier is present. Process each modifier.
            //Modifiers are separated by a ",". So we'll split them and write each one to the textbox.
            foreach (string modifier in e.Modifiers.ToString().Split(new Char[] { ',' }))
            {
                textBox9.Text += modifier + " + ";
            }

            //KEYCODE contains the last key pressed by the user.
            //If KEYCODE contains a modifier, then the user has not entered a shortcut. hence, KeyisSet is false
            //But if not, KeyisSet is true.
            if (e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.ControlKey | e.KeyCode == Keys.Menu)
            {
                //KeyisSet = false;
            }
            else
            {
                textBox9.Text += e.KeyCode.ToString();
            }
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.RegistHotKey01 = string.IsNullOrEmpty(textBox1.Text.Trim()) ? "" : textBox1.Text.Trim();
            Properties.Settings.Default.RegistHotKey02 = string.IsNullOrEmpty(textBox2.Text.Trim()) ? "" : textBox2.Text.Trim();
            Properties.Settings.Default.RegistHotKey03 = string.IsNullOrEmpty(textBox3.Text.Trim()) ? "" : textBox3.Text.Trim();
            Properties.Settings.Default.RegistHotKey04 = string.IsNullOrEmpty(textBox4.Text.Trim()) ? "" : textBox4.Text.Trim();
            Properties.Settings.Default.RegistHotKey05 = string.IsNullOrEmpty(textBox5.Text.Trim()) ? "" : textBox5.Text.Trim();
            Properties.Settings.Default.RegistHotKey06 = string.IsNullOrEmpty(textBox6.Text.Trim()) ? "" : textBox6.Text.Trim();
            Properties.Settings.Default.RegistHotKey07 = string.IsNullOrEmpty(textBox7.Text.Trim()) ? "" : textBox7.Text.Trim();
            Properties.Settings.Default.RegistHotKey08 = string.IsNullOrEmpty(textBox8.Text.Trim()) ? "" : textBox8.Text.Trim();
            Properties.Settings.Default.RegistHotKey09 = string.IsNullOrEmpty(textBox9.Text.Trim()) ? "" : textBox9.Text.Trim();
            try
            {
                int mainshelltime = Convert.ToInt32(textBox10.Text.Trim());
                Properties.Settings.Default.DelayTime = mainshelltime;
            }
            catch (Exception ex)
            {
                MessageBox.Show("时间值转换为整数失败，请填写正确的值");
                return;
            }
            Properties.Settings.Default.Save();
            MessageBox.Show("设置完成，请重新启动程序使设置生效");
        }
    }
}
